﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net.Sockets;
using System.Net;
using System.Text;

namespace SuperX.Helper
{
    public class ListenerHelper
    {
        public static byte[] SendData(NodeOperationCode Code, byte[] data, int offset, int length)
        {
            byte[] dst = new byte[length + 5];
            dst[0] = 0;
            dst[1] = (byte)((int)Code / 256);
            dst[2] = (byte)((int)Code % 256);
            dst[3] = (byte)(length / 256);
            dst[4] = (byte)(length % 256);
            Buffer.BlockCopy(data, offset, dst, 5, length);
            return dst;
        }

        public static NodeOperationCode GetOperationCode(byte[] data)
        {
            byte[] header = new byte[5];
            Array.Copy(data, header, 5);
            if (header[0].Equals(0))
            {
                NodeOperationCode opcode = (NodeOperationCode)(header[1] * 256) + header[2];
                return opcode;
            }
            return NodeOperationCode.NONE;
        }
    }

    public class SocketInfo
    {
        public Socket Socket { get; set; }

        public byte[] Buffer { get; set; } = new byte[4096];

        public EndPoint RemptePoint = new IPEndPoint(IPAddress.Any, 0);
    }

    public enum NodeOperationCode
    {
        Search = 100,
        Responding = 101,
        NONE = -1,
    }

    public enum NodeSearchModes
    {
        [Description("广播")]
        Broadcast,

        [Description("扫描")]
        Scan
    }
}